package execute;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import mp3.ManageMP3DB;
import music.ManageMusic;

public class Frame extends JFrame {
	
	static JTextField inputFolderField = new JTextField("/Volumes/MyPassportB/MusicToiTunes/Downloads", 30);
	static JTextField outputFolderField = new JTextField("/Volumes/MyPassportB/MusicToiTunes/Music", 30);
	static JTextField outputZipFolderField = new JTextField("/Volumes/MyPassportB/MusicToiTunes/BackUp", 30);
	static JFileChooser fileChooser = new JFileChooser();
	static boolean deleteOriginals;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame("Manage Music - created by Anusha Mallampati");
		JPanel pane = new JPanel();
		pane.setLayout(new FlowLayout());

		//3. Create components and put them in the frame.
		//...create emptyLabel...
		
		 
		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		inputFolderField.setSize(100, HEIGHT);
		inputFolderField.setEditable(false);
		
		JButton btn1 = new JButton("Choose Input Folder");
		 btn1.addActionListener(new ActionListener() {
			 public void actionPerformed(ActionEvent e) {
				 JFrame frame = new JFrame();
				 int retVal = fileChooser.showOpenDialog(frame);
				 inputFolderField.setText(fileChooser.getSelectedFile().getAbsolutePath());
//		            JOptionPane.showMessageDialog(frame, fileChooser.getSelectedFile().getAbsolutePath());
//				 retVal is 0 if open, 1 if close
			 }
		 });
		
		 JButton btn2 = new JButton("Choose Output Folder");
		 btn2.addActionListener(new ActionListener() {
			 public void actionPerformed(ActionEvent e) {
				 JFrame frame = new JFrame();
				 int retVal = fileChooser.showOpenDialog(frame);
				 outputFolderField.setText(fileChooser.getSelectedFile().getAbsolutePath());
//		            JOptionPane.showMessageDialog(frame, retVal);
//				 retVal is 0 if open, 1 if close
			 }
		 });
		 
		 JButton btn3 = new JButton("Choose Output Zip Folder");
		 btn3.addActionListener(new ActionListener() {
			 public void actionPerformed(ActionEvent e) {
				 JFrame frame = new JFrame();
				 int retVal = fileChooser.showOpenDialog(frame);
				 outputZipFolderField.setText(fileChooser.getSelectedFile().getAbsolutePath());
//		            JOptionPane.showMessageDialog(frame, retVal);
//				 retVal is 0 if open, 1 if close
			 }
		 });
		 
		 JLabel emptyLabel = new JLabel("Original Zips",
	                JLabel.CENTER);

		    JRadioButton deleteDownloaded = new JRadioButton("Delete");
		    deleteDownloaded.setActionCommand("true");
		    deleteDownloaded.setSelected(true);
		    deleteOriginals = true;

		    JRadioButton keepDownloaded = new JRadioButton("Keep");
		    keepDownloaded.setActionCommand("false");

		    //Group the radio buttons.
		    ButtonGroup group = new ButtonGroup();
		    group.add(deleteDownloaded);
		    group.add(keepDownloaded);
		    
		    ActionListener deleteOriginalZips = new ActionListener() {
				 public void actionPerformed(ActionEvent e) {
					 deleteOriginals = Boolean.valueOf(e.getActionCommand());
				 }
		    };

		    //Register a listener for the radio buttons.
		    deleteDownloaded.addActionListener(deleteOriginalZips);
		    keepDownloaded.addActionListener(deleteOriginalZips);
		    
		 
		 JButton cleanMusic = new JButton("Clean Music");
		 cleanMusic.addActionListener(new ActionListener() {
			 public void actionPerformed(ActionEvent e) {
				 JFrame frame = new JFrame();
//				 int retVal = fileChooser.showOpenDialog(frame);
//				 outputFolderField.setText(fileChooser.getSelectedFile().getAbsolutePath());
				ManageMusic main = new ManageMusic(inputFolderField.getText(), outputFolderField.getText(), 
						outputZipFolderField.getText());
				try {
					main.extractMusic(deleteOriginals);
					List<String> duplicateFolders = main.editMusic();
					//zip edited ones again
					main.zipEdited();
					if(duplicateFolders == null || duplicateFolders.isEmpty()) {
						JOptionPane.showMessageDialog(frame, "All Success");
					} else {
						StringBuilder duplicates = new StringBuilder("All Success, but following duplicates");
						for(String df : duplicateFolders) {
							duplicates.append("\n");
							duplicates.append(df);
						}
						JOptionPane.showMessageDialog(frame, duplicates.toString());
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(frame, "Something failed - file related");
					e1.printStackTrace();
				}
				catch (Exception e1) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(frame, "Something failed");
					e1.printStackTrace();
				}
//	            JOptionPane.showMessageDialog(frame, retVal);
//			 retVal is 0 if open, 1 if close
			 }
		 });
		 
		 JButton syncMusic = new JButton("Sync Music with DB (output folder)");
		 syncMusic.addActionListener(new ActionListener() {
			 public void actionPerformed(ActionEvent e) {
				 JFrame frame = new JFrame();
//				 int retVal = fileChooser.showOpenDialog(frame);
//				 outputFolderField.setText(fileChooser.getSelectedFile().getAbsolutePath());
				 ManageMP3DB m = new ManageMP3DB(outputFolderField.getText());
				try {
					m.syncWithDB();
					JOptionPane.showMessageDialog(frame, "All Success");
				}
				catch (Exception e1) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(frame, "Something failed");
					e1.printStackTrace();
				}
			 }
		 });
		 
		 
		 
	      
	      
	    //add  
	    pane.add(btn1);
	    pane.add(inputFolderField);
	    pane.add(btn2);
	    pane.add(outputFolderField);
	    pane.add(btn3);
	    pane.add(outputZipFolderField);
	    pane.add(emptyLabel);
	    pane.add(deleteDownloaded);
	    pane.add(keepDownloaded);
	    pane.add(cleanMusic);
	    pane.add(syncMusic);
		
		
//		fileChooser.addActionListener(new ActionListener() {
//		      public void actionPerformed(ActionEvent e) {
//		    	  textField.setText(fileChooser.getCurrentDirectory().getAbsolutePath());
//		      }
//		});
		
		
		frame.setContentPane(pane);
		//2. Optional: What happens when the frame closes?
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//4. Size the frame.
		frame.pack();
		frame.setSize(600, 200);

		//5. Show it.
		frame.setVisible(true);

	}

}
